package com.grj.template.boot.controller;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.grj.template.boot.domain.Dictionary;
import com.grj.template.boot.domain.R;
import com.grj.template.boot.service.DictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/dictionary")
@Api(tags = "字典管理控制器")
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 分页查询字典列表
     * @param currentPage 当前页码
     * @param pageSize 每页大小
     * @param dictionary 查询条件
     * @return 分页结果
     */
    @GetMapping("/page")
    @ApiOperation("分页查询字典列表")
    public R<Page<Dictionary>> fetchDictionaryPage(
            @RequestParam int currentPage,
            @RequestParam int pageSize,
            @RequestParam String dictionary,
            @RequestParam(required = false) List<Date> during
            ) {
        return R.ok(dictionaryService.fetchDictionaryPage(currentPage, pageSize, JSONUtil.toBean(dictionary, Dictionary.class), during));
    }

    /**
     * 添加字典
     * @param dictionary 字典对象
     * @return 添加结果
     */
    @PostMapping
    @ApiOperation("添加字典")
    public R<Void> postDictionary(@RequestBody @Valid Dictionary dictionary) {
        dictionaryService.postDictionary(dictionary);
        return R.okMsg("添加字典成功");
    }

    /**
     * 更新字典
     * @param dictionary 字典对象
     * @return 更新结果
     */
    @PutMapping
    @ApiOperation("更新字典")
    public R<Void> putDictionary(@RequestBody @Valid Dictionary dictionary) {
        dictionaryService.putDictionary(dictionary);
        return R.okMsg("更新字典成功");
    }

    /**
     * 删除字典
     * @param id 字典 ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除字典")
    public R<Void> delDictionary(@PathVariable Long id) {
        dictionaryService.delDictionary(id);
        return R.okMsg("删除字典成功");
    }

    /**
     * 批量删除字典
     * @param ids 字典 ID 数组
     * @return 删除结果
     */
    @DeleteMapping("/batch/delete")
    @ApiOperation("批量删除字典")
    public R<Void> batchDelDictionary(@RequestParam List<Long> ids) {
        dictionaryService.batchDelDictionary(ids);
        return R.okMsg("批量删除字典成功");
    }

    /**
     * 获取字典详情
     * @param id 字典 ID
     * @return 字典详情
     */
    @GetMapping("/{id}")
    @ApiOperation("获取字典详情")
    public R<Dictionary> fetchDictionaryById(@PathVariable Long id) {
        return R.ok(dictionaryService.fetchDictionaryById(id));
    }

}
